﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TaxEntity.User;
using TaxBiz.User;
using BaseData;
using BaseData.UI;

namespace Admin
{
    public partial class Login : System.Web.UI.Page
    {
        UserBiz ud = new UserBiz();

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string userName = this.txtUserName.Value;
            string password = this.txtPassword.Value;
            string checkCode = this.txtCheckCode.Value;
            
            if (string.IsNullOrEmpty(userName))
            {
                Jscript.Alert("用户名不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(password))
            {
                Jscript.Alert("密码不能为空！");
                return;
            } 
            if (string.IsNullOrEmpty(checkCode))
            {
                Jscript.Alert("验证码不能为空！");
                return;
            }
            if (Session["checkcode"] == null || Session["checkcode"].ToString() != checkCode)
            {
                Jscript.AlertAndGoBack("验证码错误或已过期请重新输入！");
                return;
            }
            if (!ud.Exists(userName))
            {
                Jscript.Alert("用户不存在，请重新输入！");
                return;
            }
            UserEntity ue = ud.GetEntityByName(userName);
            if (ue.Status == 0)
            {
                Jscript.Alert("帐户已经停用！");
                return;
            }
            if (ue.Pwd != ud.GetEncryptPassword(password))
            {
                Jscript.Alert("用户名或者密码不符，请重新输入！");
                return;
            }
            try
            {
                ud.UpdateLoginTime(ue.UserId);
            }
            catch (Exception ex)
            {
                string message = "登录更新登录时间出错，用户" + ue.UserName + "<br/>" + ex.Message + "<br/>" + ex.StackTrace;
                //记出错日志
            }
            TaxBiz.Log.LoginLogBiz.Add(ue, 1);
            BaseCommon.BasePage.UserloginCookie(userName, ue.UserId.ToString(), password, ue.UserEType);
            Jscript.JavaScriptLocationHref("/Admin/Default.aspx");
        }
    }
}